﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Content;
using TimeTickService.Models;

namespace TimeTickService.Controllers
{
    [Route("api/[controller]")]
    public class MqHangController : Controller
    {
        private readonly IRedisHelper _redisHelper;
        private readonly IMqHelper _mqHelper;

        public MqHangController(IMqHelper mqHelper, IRedisHelper redisHelper)
        {
            _mqHelper = mqHelper;
            _redisHelper = redisHelper;
        }

        [HttpPost("schedule")]
        public void Schedule([FromBody]AddMQScheduleModel model)
        {
            var taskId = BackgroundJob.Schedule(() => _mqHelper.EnqueueAction(model), model.Delay);
            _redisHelper.StringSet(model.Key, taskId);
        }

        [HttpGet("{key}")]
        public string GetTaskId(string key)
        {
            return _redisHelper.StringGet(key);
        }
    }
}
